package cn.com.cfae.iras.doc.analyze.components.rationality;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.handUtil.SearchKeywordUtil;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;



/**
 * 内容合理性
 * M-3-1
 * 1、待确认 搜文本 核对是否有“分销期限”字段，提示人工核对。
 * 发行阶段
 */
@Component("R1013")
@Scope("prototype")
public class R1013 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(R1013.class);
    @Autowired
    private HandingData handingData;

    @Autowired
    private SearchKeywordUtil searchKeywordUtil;

    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取R1013组件M");
        List<ComponentResult> componentResults = checkRulesR1013(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取R1013组件BM");
        List<ComponentResult> componentResults = checkRulesR1013(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取R1013组件DM");
        List<ComponentResult> componentResults = checkRulesR1013(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取R1013组件BDM");
        List<ComponentResult> componentResults = checkRulesR1013(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    private List<ComponentResult> checkRulesR1013(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = null;
        boolean flag = false;
        int startPageNum = 0;
        String keyword = ".*(分销日|分销期限).*";
        String key = "分销期限,分销日,分销期";
        try {
            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPageNum = chapterModel.getStartPage();
                if (chapterModel!=null){
                    List<PageModel> pageModelList = chapterModel.getPageModelList();
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel :pageModelList){
                            //Map<String, Object> containsKeyWord = handingData.isContainsKeyWordOfRegex(pageModel.getJsonTextData(), keyword);
                            Map<String, Object> containsKeyWord = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), key, null);
                            flag = (boolean)containsKeyWord.get("flag");
                            if (flag){
                                pageNum = pageModel.getPageNumber();
                                pageNum = pageNum!=0?pageNum:startPageNum;
                                contentAndPositionList =(List<ComponentResult.TextPosition>) containsKeyWord.get("position");
                                break;
                            }
                        }
                    }
                }
            }
            if (flag){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-分销期限","合理",false,getRegulationId(),pageNum,contentAndPositionList,"01");
                componentResults.add(componentResult);
            }else {
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-分销期限","未检查到,建议人工核对",true,getRegulationId(),startPageNum,contentAndPositionList,"03");
                componentResults.add(componentResult);
            }
        }catch (Exception e){
            componentResults.clear();
            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-分销期限","未识别,建议人工核对",true,getRegulationId(),startPageNum,contentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }

        return componentResults;
    }
}
